<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>检测 HTTP 头是否已经发送</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.headers-sent" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">headers_sent</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">headers_sent</span> &mdash; <span class="dc-title">检测 HTTP 头是否已经发送</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.headers-sent-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>headers_sent</strong></span>
    ([ <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$file</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$line</code></span>
  ]] ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   检测 HTTP 头是否已经发送。
  </p>
  <p class="para">
    HTTP 头已经发送时，就无法通过 <span class="function"><a href="header.html" class="function">header()</a></span> 添加更多头字段。
   使用此函数起码可以防止 HTTP 头出错。另一个解决方案是用 <a href="ref.outcontrol.html" class="link">输出缓冲</a>。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.headers-sent-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">file</code></dt>

     <dd>

      <p class="para">
       若设置了可选参数 <code class="parameter">file</code> and
       <code class="parameter">line</code>，
       <span class="function"><strong>headers_sent()</strong></span> 会把 PHP 文件名放在 <code class="parameter">file</code> 变量里，
       输出开始的行号放在 <code class="parameter">line</code> 变量里。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">line</code></dt>

     <dd>

      <p class="para">
       输出开始的行号。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.headers-sent-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   HTTP 头未发送时，<span class="function"><strong>headers_sent()</strong></span> 返回 <strong><code>FALSE</code></strong>，否则返回 <strong><code>TRUE</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.headers-sent-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5496">
    <p><strong>Example #1 使用 <span class="function"><strong>headers_sent()</strong></span> 的例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;没有&nbsp;HTTP&nbsp;头就发送一个<br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">headers_sent</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Location:&nbsp;http://www.example.com/'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;使用&nbsp;file&nbsp;和&nbsp;line&nbsp;参数选项的例子<br />//&nbsp;注意&nbsp;$filename&nbsp;和&nbsp;$linenum&nbsp;用于下文中使用<br />//&nbsp;所以不要提前为它们赋值<br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">headers_sent</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$linenum</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Location:&nbsp;http://www.example.com/'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br /><br /></span><span style="color: #FF8000">//&nbsp;很有可能在这里触发错误<br /></span><span style="color: #007700">}&nbsp;else&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Headers&nbsp;already&nbsp;sent&nbsp;in&nbsp;</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000">&nbsp;on&nbsp;line&nbsp;</span><span style="color: #0000BB">$linenum</span><span style="color: #DD0000">\n"&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"Cannot&nbsp;redirect,&nbsp;for&nbsp;now&nbsp;please&nbsp;click&nbsp;this&nbsp;&lt;a&nbsp;"&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"href=\"http://www.example.com\"&gt;link&lt;/a&gt;&nbsp;instead\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.headers-sent-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
<p class="para">
 数据头只会在SAPI支持时得到处理和输出。
</p>
</p></blockquote>

 </div>


 <div class="refsect1 seealso" id="refsect1-function.headers-sent-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="ob_start.html" class="function" rel="rdfs-seeAlso">ob_start()</a> - 打开输出控制缓冲</span></li>
    <li class="member"><span class="function"><a href="trigger_error.html" class="function" rel="rdfs-seeAlso">trigger_error()</a> - 产生一个用户级别的 error/warning/notice 信息</span></li>
    <li class="member"><span class="function"><a href="headers_list.html" class="function" rel="rdfs-seeAlso">headers_list()</a> - 返回已发送的 HTTP 响应头（或准备发送的）</span></li>
    <li class="member">
     <span class="function"><a href="header.html" class="function" rel="rdfs-seeAlso">header()</a> - 发送原生 HTTP 头</span> 中有更多相关细节的讨论。
    </li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>